/*
 * @Author: wangmyng wangmyng@163.com
 * @Date: 2025-11-08 21:57:26
 * @LastEditors: wangmyng wangmyng@163.com
 * @LastEditTime: 2025-11-08 23:04:23
 */
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

export interface DetailObject {
  id: string | number;
  [key: string]: any; // 允许其他字段
}

export interface DetailWindowProps {
  visible: boolean;
  x: string;
  y: string;
  width: string;
  height: string;
  title: string;
  obj: DetailObject | null; // 使用具体类型，初始为 null
}

const initialState: DetailWindowProps = {
  visible: false,
  x: "33%",
  y: "25%", 
  width: "25%",
  height: "35%",
  title: "",
  obj: null, // 初始为 null 更安全
};

// 创建 slice
const detailWindowSlice = createSlice({
  name: "detailWindow",
  initialState,
  reducers: {
    setDetailWindow: (state, action: PayloadAction<Partial<DetailWindowProps>>) => {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 关闭 DetailWindow
    closeDetailWindow: () => initialState,
    
    // 设置当前选中的对象
    setDetailWindowObj: (state, action: PayloadAction<object>) => {
      state.obj = action.payload;
    },
  },
});

// 导出 actions
export const { setDetailWindow, closeDetailWindow, setDetailWindowObj } = detailWindowSlice.actions;

// 导出 reducer
export default detailWindowSlice.reducer;

// 导出选择器
export const selectDetailWindow = (state: { detailWindow: DetailWindowProps }) => state.detailWindow;
export const selectDetailWindowObj = (state: { detailWindow: DetailWindowProps }) => state.detailWindow.obj;
export const selectDetailWindowVisible = (state: { detailWindow: DetailWindowProps }) => state.detailWindow.visible;
